{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_path = '../data/testData0626.csv'\n",
    "port_path = '../data/port.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_data_type(data):\n",
    "    data['onboardDate'] = pd.to_datetime(data['onboardDate'], infer_datetime_format=True).apply(lambda x: x.replace(tzinfo=None))\n",
    "    data['loadingOrder'] = data['loadingOrder'].astype(str)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True).apply(lambda x: x.replace(tzinfo=None))\n",
    "    data['longitude'] = data['longitude'].astype(float)\n",
    "    data['latitude'] = data['latitude'].astype(float)\n",
    "    data['speed'] = data['speed'].astype(float)\n",
    "    data['TRANSPORT_TRACE'] = data['TRANSPORT_TRACE'].astype(str)\n",
    "    data['temp_timestamp'] = data['timestamp']\n",
    "    return data\n",
    "\n",
    "def get_test_data_info(path):\n",
    "    data = pd.read_csv(path) \n",
    "    test_trace_set = data['TRANSPORT_TRACE'].unique()\n",
    "    test_order_belong_to_trace = {}\n",
    "    for item in test_trace_set:\n",
    "        orders = data[data['TRANSPORT_TRACE'] == item]['loadingOrder'].unique()\n",
    "        test_order_belong_to_trace[item] = orders\n",
    "    \n",
    "    return format_data_type(data), test_trace_set, test_order_belong_to_trace\n",
    "\n",
    "test_data_origin, test_trace_set, test_order_belong_to_trace = get_test_data_info(test_data_path)\n",
    "\n",
    "def get_port_info():\n",
    "    port_data = {}\n",
    "    test_port_set = set()\n",
    "    for route in test_trace_set:\n",
    "        ports = route.split('-')\n",
    "        test_port_set = set.union(test_port_set, set(ports))\n",
    "    port_data_origin = pd.read_csv(port_path)\n",
    "    for item in port_data_origin.itertuples():\n",
    "        if getattr(item, 'TRANS_NODE_NAME') in test_port_set:\n",
    "            port_data[getattr(item, 'TRANS_NODE_NAME')] = {'LONGITUDE': getattr(item, 'LONGITUDE'),'LATITUDE': getattr(item, 'LATITUDE') }\n",
    "    return port_data\n",
    "port_data = get_port_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "warning! positon error!   120.09385800000001 114.275347 22.58132 22.5777 2019-07-03T21:16:48.000000000 2019-07-02T04:12:48.000000000\n",
      "warning! time error!  2019-07-03T21:16:48.000000000\t2019-07-02T04:12:48.000000000\t41.06666666666667\n",
      "AE223035353902\n",
      "----------------------------------------------------------------------------------------\n",
      "AE255175252207\n",
      "----------------------------------------------------------------------------------------\n",
      "CJ649212973636\n",
      "----------------------------------------------------------------------------------------\n",
      "CM247448368157\n",
      "----------------------------------------------------------------------------------------\n",
      "CY610195685233\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2019-01-29 15:39:38\t2019-01-28T16:22:38.000000000\t23.283333333333335\n",
      "DN799599926798\n",
      "----------------------------------------------------------------------------------------\n",
      "EF427213883396\n",
      "----------------------------------------------------------------------------------------\n",
      "EH267022831344\n",
      "----------------------------------------------------------------------------------------\n",
      "FC717660279538\n",
      "----------------------------------------------------------------------------------------\n",
      "FU963630646772\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   138.47106200000002 114.275347 40.278787 22.5777 2019-04-02T02:42:28.000000000 2019-04-02T02:42:28.000000000\n",
      "GA887308394758\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.64707 114.275347 22.301262 22.5777 2019-05-14T00:32:38.000000000 2019-05-14T00:32:38.000000000\n",
      "GB519547320123\n",
      "----------------------------------------------------------------------------------------\n",
      "GB883144830650\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.64707 114.275347 22.301262 22.5777 2019-05-14T00:32:38.000000000 2019-05-14T00:32:38.000000000\n",
      "GN416043321255\n",
      "----------------------------------------------------------------------------------------\n",
      "HO283604623948\n",
      "----------------------------------------------------------------------------------------\n",
      "ID664898551540\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   138.47106200000002 114.275347 40.278787 22.5777 2019-04-02T02:42:28.000000000 2019-04-02T02:42:28.000000000\n",
      "IP452792558795\n",
      "----------------------------------------------------------------------------------------\n",
      "IT771609532687\n",
      "----------------------------------------------------------------------------------------\n",
      "JI711917472919\n",
      "----------------------------------------------------------------------------------------\n",
      "JK197178852361\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.33096499999999 114.275347 22.550127 22.5777 2019-04-03T16:53:38.000000000 2019-04-01T18:32:58.000000000\n",
      "warning! time error!  2019-04-03T16:53:38.000000000\t2019-04-01T18:32:58.000000000\t46.34444444444444\n",
      "JL270765535989\n",
      "----------------------------------------------------------------------------------------\n",
      "JL631916963952\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.983272 114.275347 22.251767 22.5777 2020-01-21T02:46:48.000000000 2020-01-21T02:46:48.000000000\n",
      "KF926106376780\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   115.361418 114.275347 22.116954999999997 22.5777 2019-01-22T11:59:48.000000000 2019-01-22T11:59:48.000000000\n",
      "KJ137025097496\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2019-01-29 15:39:38\t2019-01-28T16:22:38.000000000\t23.283333333333335\n",
      "LA312062544244\n",
      "----------------------------------------------------------------------------------------\n",
      "LC668574552789\n",
      "----------------------------------------------------------------------------------------\n",
      "MC453879978277\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.134708 114.275347 22.447083 22.5777 2019-05-08T00:13:38.000000000 2019-05-08T00:13:38.000000000\n",
      "MT936778723306\n",
      "----------------------------------------------------------------------------------------\n",
      "NB774288833388\n",
      "----------------------------------------------------------------------------------------\n",
      "NF118629481327\n",
      "----------------------------------------------------------------------------------------\n",
      "NG158658433033\n",
      "----------------------------------------------------------------------------------------\n",
      "NN919165633546\n",
      "----------------------------------------------------------------------------------------\n",
      "NT901128080270\n",
      "----------------------------------------------------------------------------------------\n",
      "OF976490392167\n",
      "----------------------------------------------------------------------------------------\n",
      "ON239409893596\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.33003000000001 114.275347 22.551243 22.5777 2020-01-22T03:25:58.000000000 2020-01-21T02:46:48.000000000\n",
      "warning! time error!  2020-01-22T03:25:58.000000000\t2020-01-21T02:46:48.000000000\t24.65277777777778\n",
      "OP643550193492\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   119.60405 114.275347 22.216907 22.5777 2019-12-31T18:55:38.000000000 2019-12-30T23:58:08.000000000\n",
      "warning! time error!  2019-12-31T18:55:38.000000000\t2019-12-30T23:58:08.000000000\t18.958333333333332\n",
      "PE777468726690\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.983272 114.275347 22.251767 22.5777 2020-01-21T02:46:48.000000000 2020-01-21T02:46:48.000000000\n",
      "QD784122359474\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.45943 114.275347 22.193138 22.5777 2019-01-16 17:45:58 2019-01-16T17:01:48.000000000\n",
      "SM358308649957\n",
      "----------------------------------------------------------------------------------------\n",
      "SQ131680632652\n",
      "----------------------------------------------------------------------------------------\n",
      "WQ478963774736\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.144247 114.275347 22.422977 22.5777 2019-05-08T02:56:28.000000000 2019-05-08T00:13:38.000000000\n",
      "WZ372398707823\n",
      "----------------------------------------------------------------------------------------\n",
      "XJ440537959887\n",
      "----------------------------------------------------------------------------------------\n",
      "XN336266061100\n",
      "----------------------------------------------------------------------------------------\n",
      "XN936397635918\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   138.47106200000002 114.275347 40.278787 22.5777 2019-04-02T02:42:28.000000000 2019-04-02T02:42:28.000000000\n",
      "XQ927384856140\n",
      "----------------------------------------------------------------------------------------\n",
      "XZ468287272542\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.33011299999998 114.275347 22.551333 22.5777 2019-11-27T09:21:38.000000000 2019-11-26T01:53:48.000000000\n",
      "warning! time error!  2019-11-27T09:21:38.000000000\t2019-11-26T01:53:48.000000000\t31.46388888888889\n",
      "YA399085613590\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.64707 114.275347 22.301262 22.5777 2019-05-14T00:32:38.000000000 2019-05-14T00:32:38.000000000\n",
      "YB183362591635\n",
      "----------------------------------------------------------------------------------------\n",
      "YB909977386589\n",
      "----------------------------------------------------------------------------------------\n",
      "YD439438277093\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.983272 114.275347 22.251767 22.5777 2020-01-21T02:46:48.000000000 2020-01-21T02:46:48.000000000\n",
      "YF408341924920\n",
      "----------------------------------------------------------------------------------------\n",
      "YG724365956065\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2019-02-05 23:45:48\t2019-02-05T16:15:28.000000000\t7.5055555555555555\n",
      "YH370554092750\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.45943 114.275347 22.193138 22.5777 2019-01-16 17:45:58 2019-01-16T17:01:48.000000000\n",
      "YJ788424984066\n",
      "----------------------------------------------------------------------------------------\n",
      "YM184398866380\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2019-02-05 23:45:48\t2019-02-05T16:15:28.000000000\t7.5055555555555555\n",
      "YQ329057128361\n",
      "----------------------------------------------------------------------------------------\n",
      "YQ484261930827\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   115.361418 114.275347 22.116954999999997 22.5777 2019-01-22T11:59:48.000000000 2019-01-22T11:59:48.000000000\n",
      "YS481380350810\n",
      "----------------------------------------------------------------------------------------\n",
      "YT729755644330\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   120.134708 114.275347 22.447083 22.5777 2019-05-08T00:13:38.000000000 2019-05-08T00:13:38.000000000\n",
      "YU978211887352\n",
      "----------------------------------------------------------------------------------------\n",
      "ZE841595716330\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   138.47106200000002 114.275347 40.278787 22.5777 2019-04-02T02:42:28.000000000 2019-04-02T02:42:28.000000000\n",
      "ZH944172279629\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.983272 114.275347 22.251767 22.5777 2020-01-21T02:46:48.000000000 2020-01-21T02:46:48.000000000\n",
      "ZJ683394838802\n",
      "----------------------------------------------------------------------------------------\n",
      "ZR256359454511\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   113.36478000000001 113.86305800000001 16.195732 22.559462 2020-03-02T22:36:48.000000000 2020-03-01T13:32:08.000000000\n",
      "warning! time error!  2020-03-02T22:36:48.000000000\t2020-03-01T13:32:08.000000000\t33.077777777777776\n",
      "AJ338174323933\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   104.10996 113.86305800000001 1.279983 22.559462 2020-03-13T20:20:58.000000000 2020-03-08T23:42:38.000000000\n",
      "warning! time error!  2020-03-13T20:20:58.000000000\t2020-03-08T23:42:38.000000000\t116.63888888888889\n",
      "BP352489313005\n",
      "----------------------------------------------------------------------------------------\n",
      "CX921020302440\n",
      "----------------------------------------------------------------------------------------\n",
      "EH417316823657\n",
      "----------------------------------------------------------------------------------------\n",
      "EO389054510734\n",
      "----------------------------------------------------------------------------------------\n",
      "EP955322386383\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.36956699999999 113.86305800000001 22.00335 22.559462 2020-02-16T09:31:18.000000000 2020-02-16T08:32:38.000000000\n",
      "ES509695023954\n",
      "----------------------------------------------------------------------------------------\n",
      "FB514237413831\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   110.85176200000001 113.86305800000001 15.670188000000001 22.559462 2019-11-01T09:27:08.000000000 2019-11-01T09:27:08.000000000\n",
      "FC483811239672\n",
      "----------------------------------------------------------------------------------------\n",
      "FM949372341953\n",
      "----------------------------------------------------------------------------------------\n",
      "FR946269565342\n",
      "----------------------------------------------------------------------------------------\n",
      "GP961997210316\n",
      "----------------------------------------------------------------------------------------\n",
      "HB691160735030\n",
      "----------------------------------------------------------------------------------------\n",
      "JN442113207024\n",
      "----------------------------------------------------------------------------------------\n",
      "KQ560412866765\n",
      "----------------------------------------------------------------------------------------\n",
      "KW183544635052\n",
      "----------------------------------------------------------------------------------------\n",
      "NB846169881475\n",
      "----------------------------------------------------------------------------------------\n",
      "NJ720087700098\n",
      "----------------------------------------------------------------------------------------\n",
      "OA140307375695\n",
      "----------------------------------------------------------------------------------------\n",
      "ON730299002639\n",
      "----------------------------------------------------------------------------------------\n",
      "PL770427547259\n",
      "----------------------------------------------------------------------------------------\n",
      "PN738417538862\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-08 23:42:38\t2020-03-08T16:24:48.000000000\t7.2972222222222225\n",
      "PO947970319459\n",
      "----------------------------------------------------------------------------------------\n",
      "QG541838587610\n",
      "----------------------------------------------------------------------------------------\n",
      "QH410560478987\n",
      "----------------------------------------------------------------------------------------\n",
      "RJ628501338267\n",
      "----------------------------------------------------------------------------------------\n",
      "RQ713676931694\n",
      "----------------------------------------------------------------------------------------\n",
      "SK705728058188\n",
      "----------------------------------------------------------------------------------------\n",
      "TG995104264426\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.28508000000001 113.86305800000001 21.392778 22.559462 2020-01-09T04:41:28.000000000 2020-01-09T00:54:28.000000000\n",
      "TH712532732793\n",
      "----------------------------------------------------------------------------------------\n",
      "TT986334415542\n",
      "----------------------------------------------------------------------------------------\n",
      "WE502281396276\n",
      "----------------------------------------------------------------------------------------\n",
      "XE746638521320\n",
      "----------------------------------------------------------------------------------------\n",
      "XG512762876103\n",
      "----------------------------------------------------------------------------------------\n",
      "XJ712428526925\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-08 23:42:38\t2020-03-08T17:42:48.000000000\t5.997222222222222\n",
      "YP456579433683\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-08 23:42:38\t2020-03-08T16:51:48.000000000\t6.847222222222222\n",
      "YY150398506143\n",
      "----------------------------------------------------------------------------------------\n",
      "ZO151269041208\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-08 00:04:18\t2020-03-07T14:49:08.000000000\t9.252777777777778\n",
      "ES859495565412\n",
      "----------------------------------------------------------------------------------------\n",
      "FQ914057883235\n",
      "----------------------------------------------------------------------------------------\n",
      "FU680644019317\n",
      "----------------------------------------------------------------------------------------\n",
      "GB583887469604\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.317857 113.86305800000001 21.683868 22.559462 2019-12-28T20:28:18.000000000 2019-12-28T20:28:18.000000000\n",
      "GO358732747064\n",
      "----------------------------------------------------------------------------------------\n",
      "HM822520935334\n",
      "----------------------------------------------------------------------------------------\n",
      "IH428269111720\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   105.483728 113.86305800000001 3.4969019999999995 22.559462 2020-01-29T11:16:48.000000000 2020-01-26T15:01:58.000000000\n",
      "warning! time error!  2020-01-29T11:16:48.000000000\t2020-01-26T15:01:58.000000000\t68.24722222222222\n",
      "IH782813270967\n",
      "----------------------------------------------------------------------------------------\n",
      "II744464224302\n",
      "----------------------------------------------------------------------------------------\n",
      "IK772954639704\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.32525 113.86305800000001 22.10385 22.559462 2020-01-01T05:18:18.000000000 2020-01-01T05:18:18.000000000\n",
      "IL935539489504\n",
      "----------------------------------------------------------------------------------------\n",
      "JR795879366910\n",
      "----------------------------------------------------------------------------------------\n",
      "JT967370009145\n",
      "----------------------------------------------------------------------------------------\n",
      "JY917130893835\n",
      "----------------------------------------------------------------------------------------\n",
      "KF763427705253\n",
      "----------------------------------------------------------------------------------------\n",
      "KX405807688251\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-07 23:24:18\t2020-03-07T14:49:08.000000000\t8.58611111111111\n",
      "LE713997921306\n",
      "----------------------------------------------------------------------------------------\n",
      "LO149448746717\n",
      "----------------------------------------------------------------------------------------\n",
      "LV542612890699\n",
      "----------------------------------------------------------------------------------------\n",
      "LY904516603549\n",
      "----------------------------------------------------------------------------------------\n",
      "MF243422538389\n",
      "----------------------------------------------------------------------------------------\n",
      "MH700243917257\n",
      "----------------------------------------------------------------------------------------\n",
      "MU538737050274\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-07 23:24:18\t2020-03-07T14:49:08.000000000\t8.58611111111111\n",
      "MV989301781753\n",
      "----------------------------------------------------------------------------------------\n",
      "MZ209373898350\n",
      "----------------------------------------------------------------------------------------\n",
      "NZ408184130946\n",
      "----------------------------------------------------------------------------------------\n",
      "OB289926624230\n",
      "----------------------------------------------------------------------------------------\n",
      "OR605684783196\n",
      "----------------------------------------------------------------------------------------\n",
      "OU967306172345\n",
      "----------------------------------------------------------------------------------------\n",
      "PC104985772716\n",
      "----------------------------------------------------------------------------------------\n",
      "PI749343957929\n",
      "----------------------------------------------------------------------------------------\n",
      "PO796918153357\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   109.874227 113.86305800000001 9.410822999999999 22.559462 2020-03-20T00:28:58.000000000 2020-03-17T03:57:28.000000000\n",
      "warning! time error!  2020-03-20T00:28:58.000000000\t2020-03-17T03:57:28.000000000\t68.525\n",
      "PU117851596971\n",
      "----------------------------------------------------------------------------------------\n",
      "QR165046694539\n",
      "----------------------------------------------------------------------------------------\n",
      "RI919621955648\n",
      "----------------------------------------------------------------------------------------\n",
      "RJ773288690736\n",
      "----------------------------------------------------------------------------------------\n",
      "RT460730850577\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.400333 113.86305800000001 21.978832999999998 22.559462 2020-03-03T13:09:08.000000000 2020-03-03T10:28:08.000000000\n",
      "SH329682133322\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.317857 113.86305800000001 21.683868 22.559462 2019-12-28T20:28:18.000000000 2019-12-28T20:28:18.000000000\n",
      "SM429651112384\n",
      "----------------------------------------------------------------------------------------\n",
      "SO431489196829\n",
      "----------------------------------------------------------------------------------------\n",
      "SP126070097709\n",
      "----------------------------------------------------------------------------------------\n",
      "TC135108526881\n",
      "----------------------------------------------------------------------------------------\n",
      "TC648840349833\n",
      "----------------------------------------------------------------------------------------\n",
      "TR583615961758\n",
      "----------------------------------------------------------------------------------------\n",
      "TX503056748352\n",
      "----------------------------------------------------------------------------------------\n",
      "VC646267361455\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.399405 113.86305800000001 22.080573 22.559462 2020-01-15T09:39:18.000000000 2020-01-15T06:17:48.000000000\n",
      "VF478825055658\n",
      "----------------------------------------------------------------------------------------\n",
      "VT428011564481\n",
      "----------------------------------------------------------------------------------------\n",
      "VT818327782433\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.1031 113.86305800000001 22.012133 22.559462 2020-01-02T13:50:38.000000000 2020-01-02T10:44:28.000000000\n",
      "WH380476077154\n",
      "----------------------------------------------------------------------------------------\n",
      "WP494393070948\n",
      "----------------------------------------------------------------------------------------\n",
      "WT578464631651\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   113.34483 113.86305800000001 17.63356 22.559462 2020-01-13T08:01:38.000000000 2020-01-12T13:54:58.000000000\n",
      "warning! time error!  2020-01-13T08:01:38.000000000\t2020-01-12T13:54:58.000000000\t18.11111111111111\n",
      "WU124534943790\n",
      "----------------------------------------------------------------------------------------\n",
      "XJ985637739160\n",
      "----------------------------------------------------------------------------------------\n",
      "XN362262801839\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-07T19:55:08.000000000\t2020-03-07T14:49:08.000000000\t5.1\n",
      "XQ276282529131\n",
      "----------------------------------------------------------------------------------------\n",
      "XR552537159084\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   112.94326299999999 113.86305800000001 16.316876999999998 22.559462 2019-12-29T14:02:08.000000000 2019-12-28T06:37:58.000000000\n",
      "warning! time error!  2019-12-29T14:02:08.000000000\t2019-12-28T06:37:58.000000000\t31.40277777777778\n",
      "XT528627851968\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.128267 113.86305800000001 22.238533 22.559462 2020-01-15T18:33:58.000000000 2020-01-15T16:42:48.000000000\n",
      "XU155855748271\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   113.92283 113.86305800000001 21.925137 22.559462 2020-02-14 18:47:28 2020-02-14T16:06:58.000000000\n",
      "XY599394383425\n",
      "----------------------------------------------------------------------------------------\n",
      "YB398587135547\n",
      "----------------------------------------------------------------------------------------\n",
      "YG100750698799\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.176833 113.86305800000001 22.199432 22.559462 2019-08-11T19:11:28.000000000 2019-08-11T17:46:18.000000000\n",
      "YJ480851991549\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.32525 113.86305800000001 22.10385 22.559462 2020-01-01T05:18:18.000000000 2020-01-01T05:18:18.000000000\n",
      "YL346384176242\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   114.298448 113.86305800000001 21.803488 22.559462 2020-03-02T11:06:48.000000000 2020-03-02T06:28:08.000000000\n",
      "ZR615135412943\n",
      "----------------------------------------------------------------------------------------\n",
      "ZV446477171810\n",
      "----------------------------------------------------------------------------------------\n",
      "FT251830039061\n",
      "----------------------------------------------------------------------------------------\n",
      "JF624982372417\n",
      "----------------------------------------------------------------------------------------\n",
      "JU425290269250\n",
      "----------------------------------------------------------------------------------------\n",
      "KO962883132036\n",
      "----------------------------------------------------------------------------------------\n",
      "LK169832200174\n",
      "----------------------------------------------------------------------------------------\n",
      "LV228770591080\n",
      "----------------------------------------------------------------------------------------\n",
      "NY964073536940\n",
      "----------------------------------------------------------------------------------------\n",
      "NZ670508767948\n",
      "----------------------------------------------------------------------------------------\n",
      "OA344403997732\n",
      "----------------------------------------------------------------------------------------\n",
      "OK727403157436\n",
      "----------------------------------------------------------------------------------------\n",
      "RB167727501375\n",
      "----------------------------------------------------------------------------------------\n",
      "XX855652295015\n",
      "----------------------------------------------------------------------------------------\n",
      "YU648769926867\n",
      "----------------------------------------------------------------------------------------\n",
      "YZ924042745853\n",
      "----------------------------------------------------------------------------------------\n",
      "ZP787001911633\n",
      "----------------------------------------------------------------------------------------\n",
      "ZQ312679418376\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   115.32633999999999 114.275347 22.23433 22.5777 2019-06-15T23:24:28.000000000 2019-06-15T23:24:28.000000000\n",
      "ZR226703204934\n",
      "----------------------------------------------------------------------------------------\n",
      "FX658315757892\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   124.657723 113.86305800000001 16.974258 22.559462 2019-05-08T00:32:58.000000000 2019-05-08T00:32:58.000000000\n",
      "GM373318338848\n",
      "----------------------------------------------------------------------------------------\n",
      "HE273820450227\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   115.01946699999999 113.86305800000001 21.876967 22.559462 2019-04-01T03:44:48.000000000 2019-04-01T03:44:48.000000000\n",
      "HQ298919518932\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   151.29096 113.86305800000001 -8.315493 22.559462 2019-04-01T00:48:48.000000000 2019-04-01T00:48:48.000000000\n",
      "HV119475836181\n",
      "----------------------------------------------------------------------------------------\n",
      "KN496453387986\n",
      "----------------------------------------------------------------------------------------\n",
      "MZ696790961139\n",
      "----------------------------------------------------------------------------------------\n",
      "NC185398487640\n",
      "----------------------------------------------------------------------------------------\n",
      "NM983847866824\n",
      "----------------------------------------------------------------------------------------\n",
      "ST695058399781\n",
      "----------------------------------------------------------------------------------------\n",
      "XX932009788203\n",
      "----------------------------------------------------------------------------------------\n",
      "YG453695887086\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   172.497467 113.86305800000001 -31.679890000000004 22.559462 2019-11-01T02:19:18.000000000 2019-11-01T02:19:18.000000000\n",
      "YP105149249992\n",
      "----------------------------------------------------------------------------------------\n",
      "YU488016730112\n",
      "----------------------------------------------------------------------------------------\n",
      "HJ246261379392\n",
      "----------------------------------------------------------------------------------------\n",
      "HV544902512699\n",
      "----------------------------------------------------------------------------------------\n",
      "JU360847167491\n",
      "----------------------------------------------------------------------------------------\n",
      "KM466086744301\n",
      "----------------------------------------------------------------------------------------\n",
      "KW203223353208\n",
      "----------------------------------------------------------------------------------------\n",
      "QD564688243325\n",
      "----------------------------------------------------------------------------------------\n",
      "HS534528541188\n",
      "----------------------------------------------------------------------------------------\n",
      "IS858244370815\n",
      "----------------------------------------------------------------------------------------\n",
      "LE287046397282\n",
      "----------------------------------------------------------------------------------------\n",
      "LH639522327454\n",
      "----------------------------------------------------------------------------------------\n",
      "MJ441989041126\n",
      "----------------------------------------------------------------------------------------\n",
      "MV936703089625\n",
      "----------------------------------------------------------------------------------------\n",
      "IK635822778695\n",
      "----------------------------------------------------------------------------------------\n",
      "ZZ524449869421\n",
      "----------------------------------------------------------------------------------------\n",
      "IT779801656093\n",
      "----------------------------------------------------------------------------------------\n",
      "LW647099390002\n",
      "----------------------------------------------------------------------------------------\n",
      "OI773482181763\n",
      "----------------------------------------------------------------------------------------\n",
      "UG437248887327\n",
      "----------------------------------------------------------------------------------------\n",
      "VR369313473918\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2020-03-23 10:21:58\t2020-03-22T23:25:38.000000000\t10.938888888888888\n",
      "XQ938421543568\n",
      "----------------------------------------------------------------------------------------\n",
      "YM636162276376\n",
      "----------------------------------------------------------------------------------------\n",
      "YC498927281293\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   104.5341 113.653433 1.4994180000000001 22.694213 2019-05-08T17:49:58.000000000 2019-05-08T17:49:58.000000000\n",
      "YC652110864219\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! time error!  2019-02-06 17:41:18\t2019-02-06T07:57:18.000000000\t9.733333333333333\n",
      "YF176657816855\n",
      "----------------------------------------------------------------------------------------\n",
      "warning! positon error!   104.196312 114.13970900000001 1.287627 22.419915 2019-04-01T00:14:18.000000000 2019-04-01T00:14:18.000000000\n",
      "YI904717006355\n",
      "----------------------------------------------------------------------------------------\n",
      "YX999785784083\n",
      "----------------------------------------------------------------------------------------\n",
      "ZA229272050987\n",
      "----------------------------------------------------------------------------------------\n",
      "ZS498116910903\n",
      "----------------------------------------------------------------------------------------\n",
      "50 24\n"
     ]
    }
   ],
   "source": [
    "cnt1 = 0\n",
    "cnt2 = 0\n",
    "for route in test_trace_set:\n",
    "    start_port = route.split('-')[0]\n",
    "    start_lon = port_data[start_port]['LONGITUDE']\n",
    "    start_lat = port_data[start_port]['LATITUDE']\n",
    "    for order in test_order_belong_to_trace[route]:\n",
    "        order_info_set = test_data_origin[test_data_origin['loadingOrder'] == order].sort_values(by='timestamp')\n",
    "        onboardDate = test_data_origin[test_data_origin['loadingOrder'] == order]['onboardDate'].unique()[0]\n",
    "        start_time = test_data_origin[test_data_origin['loadingOrder'] == order]['timestamp'].unique()[0]\n",
    "        for row in order_info_set.itertuples():\n",
    "            if abs(row.longitude-start_lon) < 0.3 and abs(row.latitude-start_lat) < 0.3 and row.speed > 0:\n",
    "                start_time = row.timestamp\n",
    "                break  \n",
    "        row = order_info_set.head(1)\n",
    "\n",
    "        if (abs((row.longitude-start_lon).values[0]) > 0.3 or abs((row.latitude-start_lat).values[0]) > 0.3) and start_time == onboardDate:\n",
    "            print('warning! positon error!  ', row.longitude.values[0], start_lon, row.latitude.values[0], start_lat, start_time, onboardDate)\n",
    "            cnt1 = cnt1 + 1\n",
    "        if start_time != onboardDate and int(pd.Timedelta(start_time-onboardDate).total_seconds()/3600) > 4:\n",
    "            mat = \"warning! time error!  {}\\t{}\\t{}\"\n",
    "            print(mat.format(start_time, onboardDate, pd.Timedelta(start_time-onboardDate).total_seconds()/3600))\n",
    "            cnt2 = cnt2 + 1\n",
    "        print(order)\n",
    "        print('----------------------------------------------------------------------------------------')\n",
    "        \n",
    "print(cnt1, cnt2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
